require(['loading'], function(loading) {
    var selectTab = localStorage.getItem('scselectTab');
    if(selectTab == null || selectTab == ''){
        selectTab = 'gyxx';
    }
    var gyxxOver=false;
    var cdOver=false;
    var gyxxpageNo=1;
    var cdpageNo=1;
    var gyxxSearch = true;
    var cdSearch = true;
    //应用实例化
    var myCollection = new Vue({
        el: '#myCollection',
        data: {
            gyxxList: [],
            cdList:[],
            isgyxx:selectTab=='gyxx',
            showLoading: false,
            gyxxUrl: wxUrl + '/v3.2/collection/supplymessagelist',
            cdUrl: wxUrl + '/v3.2/collection/farmlist',
            gyxxitem: {
                params:{
                    token: token,
                    userToken: userToken,
                    pageNo:gyxxpageNo,
                    lat:latitude,
                    lng:longitude,
                    pageSize:10
                }
            },
            cditem: {
                params:{
                    token: token,
                    userToken: userToken,
                    pageNo:cdpageNo,
                    lat:latitude,
                    lng:longitude,
                    pageSize:10
                }
            }
        },
        ready: function() {
            bindScroll();
            bindTabClick();
            this.getgyxxList();
            this.getcdList();
            bindItemSwipe();
        },
        methods: {
            getgyxxList: function() {
                if(gyxxSearch){
                    gyxxSearch = false;
                }else{
                    return;
                }
                this.$http.get(this.gyxxUrl, this.gyxxitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var gyxxlist = result.data.list;
                            if(gyxxlist){
                                this.gyxxList=this.gyxxList.concat(gyxxlist);
                            }
                            if(result.data.pageNo>=result.data.totalPage){
                                gyxxOver=true;
                            }
                            gyxxpageNo++;
                            this.gyxxitem.params.pageNo=gyxxpageNo;
                        }
                        gyxxSearch = true;
                    },function(response) {
                        gyxxSearch = true;
                    });
            },
            getcdList: function() {
                if(cdSearch){
                    cdSearch = false;
                }else{
                    return;
                }
                this.$http.get(this.cdUrl, this.cditem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var cdlist = result.data.list;
                            if(cdlist){
                                this.cdList=this.cdList.concat(cdlist);
                            }
                            if(result.data.pageNo>=result.data.totalPage){
                                cdOver=true;
                            }
                            cdpageNo++;
                            this.cditem.params.pageNo=cdpageNo;
                        }
                        cdSearch = true;
                    },function(response) {
                        cdSearch = true;
                    });
            }
        }
    });

    function bindTabClick(){
        $('.tab_1 li').click(function() {
            var siblings = $(this).siblings();
            var index = $(this).index();
            if(siblings.length > 0) {
                $(this).addClass('bg');
                siblings.removeClass("bg");
                $(".cont_1 .tab").eq(index).addClass('xian').siblings().removeClass("xian");
                selectTab=$(".cont_1 .tab").eq(index).attr("select-data");
                localStorage.setItem("scselectTab", selectTab);
            }
            $(".item").removeClass('selected');
        });
    }

    function bindItemSwipe(){
        $("#mainul").on("swipeleft",".item", function(){
            $(this).addClass('selected').siblings().removeClass('selected');
            $(this).find("a.remove").on("click",function(){
                //这里需要获取id,删除
                var itemData =  $(this).parent().attr("item-data");
                $("#delete").show();
                $(".btn-success").click(function(){
                    var ajaxUrl = '';
                    var ajaxData={token: token,userToken: userToken,action:0};
                    if(selectTab=='gyxx'){
                        ajaxUrl=wxUrl+'/v3.2/collection/supplymessage';
                        ajaxData.supplyId=itemData;
                    }else{
                        ajaxUrl=wxUrl+'/v3.2/collection/farm';
                        ajaxData.farmId=itemData;
                    }
                    if(itemData){
                        $.ajax({
                          type: 'POST',
                          url: ajaxUrl,
                          data: ajaxData,
                          success: function(){
                            $("#delete").hide();
                            $(".item[item-data='"+itemData+"']").remove();
                          }
                        });
                    }
                });
                $(".btn-default").click(function () {
                    $("#delete").hide();
                    $(".item").removeClass('selected');
                });
            });
        }).on("swiperight", function(){
            $(".item").removeClass('selected');
        });
    }

    function bindScroll(){
        window.addEventListener('scroll', function() {
            $(".item").removeClass('selected');
            if($(window).scrollTop() + $(window).height() == $(document).height()) {
                if(selectTab=='gyxx'){
                    if(!gyxxOver){
                        myCollection.getgyxxList();
                    }
                }else{
                    if(!cdOver){
                        myCollection.getcdList();
                    }
                }
                
            }
        });
    }
});
// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
    localStorage.removeItem("scselectTab");
	goBackVHistory();
});